#include "dataconverttool.h"

namespace Common {

DataConvertTool::DataConvertTool(QObject *parent)
    : QObject(parent)
{

}

DataConvertTool::~DataConvertTool()
{

}

QByteArray DataConvertTool::floatToByteArray(float num, bool isBigEndian)
{
    QByteArray arr(4, 0);
    PacketFloat a;
    a.f = num;
    if (isBigEndian)
    {
        arr[0] = static_cast<uchar>(a.buf[3] & 0xff);
        arr[1] = static_cast<uchar>(a.buf[2] & 0xff);
        arr[2] = static_cast<uchar>(a.buf[1] & 0xff);
        arr[3] = static_cast<uchar>(a.buf[0] & 0xff);
    }
    else
    {
        arr[0] = static_cast<uchar>(a.buf[0] & 0xff);
        arr[1] = static_cast<uchar>(a.buf[1] & 0xff);
        arr[2] = static_cast<uchar>(a.buf[2] & 0xff);
        arr[3] = static_cast<uchar>(a.buf[3] & 0xff);
    }

    return arr;
}

float DataConvertTool::byteToArrayFloat(QByteArray arr, bool isBigEndian)
{
    if (arr.size() != 4)
    {
        return 0.0;
    }
    PacketFloat a;

    if (isBigEndian)
    {
        a.buf[0] = arr[3];
        a.buf[1] = arr[2];
        a.buf[2] = arr[1];
        a.buf[3] = arr[0];
    }
    else
    {
        a.buf[0] = arr[0];
        a.buf[1] = arr[1];
        a.buf[2] = arr[2];
        a.buf[3] = arr[3];
    }

    return a.f;
}

QString DataConvertTool::floatToStringHex(float num, bool isBigEndian)
{
    PacketFloat a;
    a.f = num;
    QString hexStr;
    if (isBigEndian)
    {
        hexStr = intToStringHex(a.buf[3], 2).toUpper() + intToStringHex(a.buf[2], 2).toUpper()
            + intToStringHex(a.buf[1], 2).toUpper() + intToStringHex(a.buf[0], 2).toUpper();
    }
    else
    {
        hexStr = intToStringHex(a.buf[0], 2).toUpper() + intToStringHex(a.buf[1], 2).toUpper()
            + intToStringHex(a.buf[2], 2).toUpper() + intToStringHex(a.buf[3], 2).toUpper();
    }

    return hexStr;
}

float DataConvertTool::stringHexToFloat(QString strHex, bool isBigEndian)
{
    if (strHex.size() != 8)
    {
        return 0.0;
    }

    PacketFloat a;
    QString a1 = strHex.mid(0, 2);
    QString a2 = strHex.mid(2, 2);
    QString a3 = strHex.mid(4, 2);
    QString a4 = strHex.mid(6, 2);

    if (isBigEndian)
    {
        a.buf[3] = stringHexToInt(a1);
        a.buf[2] = stringHexToInt(a2);
        a.buf[1] = stringHexToInt(a3);
        a.buf[0] = stringHexToInt(a4);
    }
    else
    {
        a.buf[0] = stringHexToInt(a1);
        a.buf[1] = stringHexToInt(a2);
        a.buf[2] = stringHexToInt(a3);
        a.buf[3] = stringHexToInt(a4);
    }

    return a.f;
}

QByteArray DataConvertTool::intToByteArray(int num, bool isBigEndian)
{
    QByteArray arr(4, 0);

    if (isBigEndian)
    {
        arr[0] = static_cast<uchar>(num >> 24 & 0xff);
        arr[1] = static_cast<uchar>(num >> 16 & 0xff);
        arr[2] = static_cast<uchar>(num >> 8 & 0xff);
        arr[3] = static_cast<uchar>(num >> 0 & 0xff);
    }
    else
    {
        arr[0] = static_cast<uchar>(num >> 0 & 0xff);
        arr[1] = static_cast<uchar>(num >> 8 & 0xff);
        arr[2] = static_cast<uchar>(num >> 16 & 0xff);
        arr[3] = static_cast<uchar>(num >> 24 & 0xff);
    }

    return arr;
}

int DataConvertTool::byteArrayToInt(QByteArray arr, bool isBigEndian)
{
    if (arr.size() != 4)
    {
        return 0;
    }

    int value;
    if (isBigEndian)
    {
        value = ((static_cast<int>(arr[0]) & 0xff) << 24) +
            ((static_cast<int>(arr[1]) & 0xff) << 16) +
            ((static_cast<int>(arr[2]) & 0xff) << 8) +
            ((static_cast<int>(arr[3]) & 0xff) << 0);
    }
    else
    {
        value = ((static_cast<int>(arr[3]) & 0xff) << 24) +
            ((static_cast<int>(arr[2]) & 0xff) << 16) +
            ((static_cast<int>(arr[1]) & 0xff) << 8) +
            ((static_cast<int>(arr[0]) & 0xff) << 0);
    }

    return value;
}

QString DataConvertTool::intToStringHex(int num, int len, bool isBigEndian)
{
    QString hexStr = QString::number(num, 16);
    while (hexStr.length() < len)
    {
        hexStr.insert(0, '0');
    }

    int size = hexStr.length();
    QString dest;
    if (isBigEndian)
    {
        if (size > len)
        {
            hexStr.remove(0, size - len);
        }
        dest = hexStr;
    }
    else
    {
        if (size % 2 != 0)
        {
            hexStr = "0" + hexStr;
        }

        int len = size / 2;
        for (int i = len - 1; i >= 0; i--)
        {
            QString item = hexStr.mid(i * 2, 2);
            dest += item;
        }
    }

    return dest;
}

int DataConvertTool::stringHexToInt(QString hexStr, bool isBigEndian)
{
    unsigned int x;
    QString dest;
    if (isBigEndian)
    {
        dest = hexStr;
    }
    else
    {
        int len = hexStr.length() / 2;
        for (int i = len - 1; i >= 0; i--)
        {
            QString item = hexStr.mid(i * 2, 2);
            dest += item;
        }
    }

    int num = dest.toInt(nullptr, 16);
    return num;
}

}// namespace Common
